{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Odo migrates between many formats. `CSV <=> JSON <=> HDFS <=> SQL <=> etc`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pyspark.context.SparkContext at 0x108ff8850>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from odo import odo\n",
    "import pandas as pd\n",
    "\n",
    "csv_dataset = \"/Users/RichardAfolabi/myGitHub/Data_Science_Harvard/2014_data/countries.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Country</th>\n",
       "      <th>Region</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Algeria</td>\n",
       "      <td>AFRICA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Angola</td>\n",
       "      <td>AFRICA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Benin</td>\n",
       "      <td>AFRICA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Botswana</td>\n",
       "      <td>AFRICA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Burkina</td>\n",
       "      <td>AFRICA</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Country  Region\n",
       "0   Algeria  AFRICA\n",
       "1    Angola  AFRICA\n",
       "2     Benin  AFRICA\n",
       "3  Botswana  AFRICA\n",
       "4   Burkina  AFRICA"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Convert from CSV to Dataframe\n",
    "df = odo(csv_dataset, pd.DataFrame)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(u'Algeria', u'AFRICA'),\n",
       " (u'Angola', u'AFRICA'),\n",
       " (u'Benin', u'AFRICA'),\n",
       " (u'Botswana', u'AFRICA'),\n",
       " (u'Burkina', u'AFRICA')]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Convert from CSV to List\n",
    "odo(csv_dataset, list)[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Convert from dataframe to JSON and dump in directory\n",
    "# odo(df, 'json_dataset.json')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Row(Country=u'Algeria', Region=u'AFRICA'),\n",
       " Row(Country=u'Angola', Region=u'AFRICA'),\n",
       " Row(Country=u'Benin', Region=u'AFRICA'),\n",
       " Row(Country=u'Botswana', Region=u'AFRICA'),\n",
       " Row(Country=u'Burkina', Region=u'AFRICA')]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Use Spark SQL Context to read json file\n",
    "sp_df = sqlContext.read.json('json_dataset.json').take(5)\n",
    "sp_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Create Spark SchemaRDD using Odo\n",
    "data = [('Alice', 300.0), ('Bob', 200.0), ('Donatello', -100.0)]\n",
    "# odo(data, sqlContext.sql, dshape='var * {Country: string, Region: string}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
